import {
	getProdStatus
} from '@/module/product/prodMet';
import {
	countTime
} from '@/utils/moment/index.js';
export const downTime = {
	data() {
		return {
			prodState: {
				type: 0, // 0未开始，1进行中，2已结束
				times: 0 // 初始化查询是，距离下个状态的时间戳差值
			}, // 状态
			timeObj: {}, // 倒计时时间对象
			time: null
		}
	},

	methods: {
		getStatus(start_date, end_date) {
			if (this.time) {
				clearTimeout(this.time);
			}
			let prodState = getProdStatus(start_date, end_date, false);
			this.prodState = {
				...prodState
			};
			this.$emit('getStatus', prodState.type)
			if (prodState.type != 2) {
				// 抢购商品，且未结束开启倒计时
				this.getCountTime(prodState.times);
			}
		},
		// 定时器循环倒计时
		getCountTime(nextTime) {
			if (nextTime >= 0) {
				this.timeObj = countTime(nextTime);
				this.time = setTimeout(() => {
					this.getCountTime(this.timeObj.nextTime);
				}, 1000);
			} else {
				this.downFinish()
			}
		},
	}

}
